package com.fruit.message;

import cn.hutool.json.JSONUtil;
import com.fruit.ProductInfoOutPut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

/**
 * @Description: 接收商品服务消息/product/listForOder
 * @Author: LJ
 * @Date: 2018/11/5 16:54
 * @version:
 */
@Component
@Slf4j
@Transactional
public class ProductInfoReceiver {
    private  static final String PRODUCT_TEMPLATE = "product_%s";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queuesToDeclare = @Queue("productInfo"))
    public  void  process(String message){
        ProductInfoOutPut productInfoOutPut =  JSONUtil.toBean(message,ProductInfoOutPut.class);
        log.info("从队列[{}]接收到消息:{}" ,"productInfo",message);
        //把信息保存到redis
        stringRedisTemplate.opsForValue().set(String.format(PRODUCT_TEMPLATE,productInfoOutPut.getId()),productInfoOutPut.getName());
    }
}
